package com.aruba.shaderapplication;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposeShader;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.RadialGradient;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public class BitmapShaderView extends View {
    private Paint paint = new Paint();
    private Bitmap bitmap;
    
    public BitmapShaderView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint.setDither(true);
        bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.xyjy2);
    }

    @Override
    protected void onDraw(Canvas canvas) {
//        drawClampBitmapShader(canvas);
//        drawMirrorBitmapShader(canvas);
//        drawRepeatBitmapShader(canvas);
//        drawCircleBitmapShader(canvas);
//        drawCircleMatrixBitmapShader(canvas,250);
//        drawLinearGradient(canvas);
//        drawSweepGradient(canvas);
//        drawRadialGradient(canvas);
        drawComposeShader(canvas);
    }

    /**
     * Clamp模式下的BitmapShader
     * @param canvas
     */
    private void drawClampBitmapShader(Canvas canvas){
        BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP,Shader.TileMode.CLAMP);
        paint.setShader(bitmapShader);

        RectF rect = new RectF(0,0,1000,1000);
        canvas.drawRect(rect,paint);
    }

    /**
     * Mirror模式下的BitmapShader
     * @param canvas
     */
    private void drawMirrorBitmapShader(Canvas canvas) {
        BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.MIRROR,Shader.TileMode.MIRROR);
        paint.setShader(bitmapShader);

        RectF rect = new RectF(0,0,1000,1000);
        canvas.drawRect(rect,paint);
    }

    /**
     * Repeat模式下的BitmapShader
     * @param canvas
     */
    private void drawRepeatBitmapShader(Canvas canvas) {
        BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.REPEAT,Shader.TileMode.REPEAT);
        paint.setShader(bitmapShader);

        RectF rect = new RectF(0,0,1000,1000);
        canvas.drawRect(rect,paint);
    }
    
    /**
     * 圆形头像
     * @param canvas
     */
    private void drawCircleBitmapShader(Canvas canvas){
        BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP,Shader.TileMode.CLAMP);
        paint.setShader(bitmapShader);

        //获取半径
        int radius = Math.min(bitmap.getWidth(),bitmap.getHeight());
        RectF rect = new RectF(0,0,radius,radius);
        //画椭圆型
        canvas.drawOval(rect,paint);
    }

    /**
     * 缩放圆形头像
     * @param canvas
     */
    private void drawCircleMatrixBitmapShader(Canvas canvas,int radius){
        BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP,Shader.TileMode.CLAMP);
        paint.setShader(bitmapShader);
        Matrix matrix = new Matrix();
        //获取缩放比较小的，以保持缩放比一致，防止拉伸图片
        float scale = Math.min((float) radius/bitmap.getWidth(),(float) radius/bitmap.getHeight());
        matrix.setScale(scale,scale);
        bitmapShader.setLocalMatrix(matrix);

        RectF rect = new RectF(0,0,radius,radius);
        //画椭圆型
        canvas.drawOval(rect,paint);
    }

   private int[] colors = new int[]{Color.RED,Color.GREEN,Color.BLUE};
    /**
     * 线性渲染
     * @param canvas
     */
    private void drawLinearGradient(Canvas canvas){
        LinearGradient linearGradient = new LinearGradient(0,0,400,400,colors,null, Shader.TileMode.CLAMP);
        paint.setShader(linearGradient);

        canvas.drawRect(0,0,400,400,paint);
    }

    /**
     * 梯度渲染/渐变渲染
     * @param canvas
     */
    private void drawSweepGradient(Canvas canvas){
        SweepGradient sweepGradient = new SweepGradient(200,200,colors,null);
        paint.setShader(sweepGradient);

        canvas.drawRect(0,0,400,400,paint);
    }

    /**
     * 环形渲染
     * @param canvas
     */
    private void drawRadialGradient(Canvas canvas){
        RadialGradient radialGradient = new RadialGradient(200,200,200,colors,null, Shader.TileMode.CLAMP);
        paint.setShader(radialGradient);

        canvas.drawRect(0,0,500,500,paint);
    }

    /**
     * 组合渲染
     * @param canvas
     */
    private void drawComposeShader(Canvas canvas){
        BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP,Shader.TileMode.CLAMP);
        SweepGradient sweepGradient = new SweepGradient(200,200,colors,null);
        ComposeShader composeShader = new ComposeShader(bitmapShader,sweepGradient, PorterDuff.Mode.ADD);
        paint.setShader(composeShader);

        canvas.drawRect(0,0,1000,1000,paint);
    }
}
